//
//  XHOwnViewController.m
//  elxc
//
//  Created by 冷山 on 16/3/14.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHOwnViewController.h"
#import "XHSpeechViewController.h"
#import "XHMyDownViewController.h"
#import "XHUImageViewController.h"
#import "XHUIImageTestViewController.h"
#import "XHTestViewController.h"
#import "XHMapViewController.h"
#import "XHAnimateViewController.h"
#import "XHDiscoverViewController.h"

#import <testApp-Swift.h>

#import "XHOwnCell.h"
#import "XHOwnHeaderCell.h"

#import "Services.h"
#import "APIKey.h"
#import "AppDelegate.h"
#import "ImagePickerAction.h"
#import <UINavigationController+FDFullscreenPopGesture.h>

#import "UINavigationBar+Awesome.h"
#import "UINavigationController+YNBase.h"
#import <Realm.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import <UIAlertView-Blocks/UIAlertView+Blocks.h>
#import <UIActionSheet+Blocks.h>
#import <UIAlertView+Blocks.h>

#define SC 0

@interface XHOwnViewController ()
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) ImagePickerAction *imagePickerAction;
@property (strong, nonatomic) UIView *headView;
@end

@implementation XHOwnViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.fd_prefersNavigationBarHidden = YES;
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHOwnCell" bundle:nil] forCellReuseIdentifier:XHOwnCellIdentifier];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHOwnHeaderCell" bundle:nil] forCellReuseIdentifier:XHOwnHeaderCelldentifier];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
    
    
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarBottomLineHide:YES];
    [self.navigationController.navigationBar setTranslucent:YES];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent=NO;
    [self.navigationController setNavigationBarBottomLineHide:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=0;
//    if(section==0)
//    {
//        rows=1;
//    }
//    else
//    {
        rows=8;
//    }
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if(indexPath.section==0 && indexPath.row==0)
    {
        cell = [self ownHeaderCell];
    }
    else
    {
        cell = [self ownCell:indexPath];
    }
    
    return cell;
}

- (XHOwnHeaderCell*)ownHeaderCell
{
    XHOwnHeaderCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHOwnHeaderCelldentifier];
    cell.ui_userNumber.text = @"15929922717";
    
    return cell;
}

- (XHOwnCell*)ownCell:(NSIndexPath*)indexPath
{
    XHOwnCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHOwnCellIdentifier];
    cell.ui_moneyLable.text = @"";
    if (indexPath.row==0)
    {
        cell.ui_titleLabel.text = @"我的下载";
    }
    else if (indexPath.row==1)
    {
        cell.ui_titleLabel.text = @"语音识别";
    }
    else if (indexPath.row==2)
    {
        cell.ui_titleLabel.text = @"Swift页面";
    }
    else if (indexPath.row==3)
    {
        cell.ui_titleLabel.text = @"图片圆角优化";
    }
    else if (indexPath.row==4)
    {
        cell.ui_titleLabel.text = @"普通圆角";
    }
    else if (indexPath.row==5)
    {
        cell.ui_titleLabel.text = @"测试页面";
    }
    else if (indexPath.row==6)
    {
        cell.ui_titleLabel.text = @"地图";
    }
    else if (indexPath.row==7)
    {
        cell.ui_titleLabel.text = @"翻转动画";
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.section==1)
    {
        if (indexPath.row==0)
        {
            XHMyDownViewController * viewController = [[XHMyDownViewController alloc]init];
            viewController.title =@"我的下载";
            viewController.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:viewController animated:YES];
        }
        else if (indexPath.row==1)
        {
            XHSpeechViewController * viewController = [[XHSpeechViewController alloc]init];
            viewController.title =@"语音识别";
            viewController.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:viewController animated:YES];
        }
        else if (indexPath.row==2)
        {
            XHDiscoverViewController * viewController = [[XHDiscoverViewController alloc]init];
            viewController.title=@"资讯";
            viewController.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:viewController animated:YES];
        }
        else if (indexPath.row==3)
        {
            XHUImageViewController * viewController = [[XHUImageViewController alloc]init];
            viewController.title=@"图片圆角优化";
            viewController.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:viewController animated:YES];
        }
        else if (indexPath.row==4)
        {
            XHUIImageTestViewController *viewController =
            [[XHUIImageTestViewController alloc]init];
            viewController.title=@"普通圆角";
            viewController.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:viewController animated:YES];
        }
        else if (indexPath.row==5)
        {
            XHTestViewController *viewController =
            [[XHTestViewController alloc]init];
            viewController.title=@"测试页面";
            viewController.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:viewController animated:YES];
        }
        else if (indexPath.row==6)
        {
            XHMapViewController * viewController = [[XHMapViewController alloc]init];
            viewController.title=@"地图";
            viewController.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:viewController animated:YES];
        }
        else if (indexPath.row==7)
        {
            XHAnimateViewController * viewController = [[XHAnimateViewController alloc]init];
            viewController.title=@"动画";
            viewController.hidesBottomBarWhenPushed=YES;
            [self.navigationController pushViewController:viewController animated:YES];
        }
    }
    else if(indexPath.section==0)
    {
//        if (!self.imagePickerAction)
//        {
//            self.imagePickerAction = [[ImagePickerAction alloc] initWithShowViewController:self];
//        }
        WS(weakSelf)
//        [self.imagePickerAction ownShowImageHeaderPickWithPhotoLibraryBlock:^(NSArray *imageArray) {
//            UIImage * pickerImage = (UIImage*)imageArray[0];
            UIImage *ben = [UIImage imageNamed:@"laoshi"];
            [PromptAction showWaitPrompt:@"上传中..."];
            [weakSelf.tabBarController.view setUserInteractionEnabled:NO];
//                    [ServiceManager uploadImage:ben success:^(NSDictionary *data) {
//                        [PromptAction showSuccessPrompt:@"上传成功"];
//                        [weakSelf.tabBarController.view setUserInteractionEnabled:YES];
//                    } failure:^{
//                        [weakSelf.tabBarController.view setUserInteractionEnabled:YES];
//                    }];
            [ServiceManager uploadImage:ben success:^(id dataObject)
             {
                 [PromptAction showSuccessPrompt:@"上传成功"];
                 [self dismissViewControllerAnimated:NO completion:^{
                     [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:nil];
                 }];
                 [weakSelf.tabBarController.view setUserInteractionEnabled:YES];
                 
             } failure:^{
                 
                 [weakSelf.tabBarController.view setUserInteractionEnabled:YES];
             }];
            
//        }];

    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=55;
    if (indexPath.section==0&&indexPath.row==0)
    {
        height=161;
    }
    return height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0;
    if (section == 0)
    {
        height = 0.000001;
    }
    else
    {
        height = 10;
    }
    return height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
    CGFloat height = 5;
    return height;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat y = scrollView.contentOffset.y;
    if (y < -98*SC) { //下拉时调整坐标和高度
        CGRect rect = _headView.frame;
        rect.origin.y = y;
        rect.size.height = -y;
        _headView.frame = rect;
        UIImageView *bannerImage = (id)[_headView viewWithTag:1];
        bannerImage.frame = _headView.bounds;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
